package com.ts.code.creator.creator.impl;

import com.ts.code.creator.enums.CreateFileEnum;
import com.ts.code.creator.express.ExpressContext;
import com.ts.code.creator.express.ExpressUtil;
import com.ts.code.creator.util.CreateCodeContext;
import com.ts.code.creator.util.StringUtil;

/**
 * 聚合层 controller
 */
public class ServiceControllerCreator extends AbstractCodeCreator {

    public ServiceControllerCreator(CreateCodeContext context) {
        super(context);
    }

    @Override
    protected CreateFileEnum createFileEnum() {
        return CreateFileEnum.SERVICE_CONTROLLER;
    }

    @Override
    public void doCreate() {
        // 服务层 controller
        addMethod(classFileSearch.serviceController(), serviceController(context));
    }

    private String serviceController(CreateCodeContext context) {
        String text = "@Override\n" +
                "    public Result<${respDTO}> ${methodName}(${serviceParamStr}) {\n" +
                "        return Result.success(${entityNameLower}Service.${methodName}(${reqDTO}));\n" +
                "    }";

        String reqDTO = reqDTO();
        ExpressContext expressContext = new ExpressContext();
        expressContext.put(context);
        expressContext.put("respDTO", respDTO());
        expressContext.put("returnType", adminVO());
        expressContext.put("serviceParamStr", reqDTO);
        expressContext.put("entityNameLower", StringUtil.firstLower(context.getEntityName()));
        expressContext.put("reqDTO", StringUtil.isNotBlank(reqDTO) ? "reqDTO" : "");
        return ExpressUtil.eval(text, expressContext);
    }

}
